<?php


namespace App\Store\Models\OldMaterial;

use App\Common\Services\Common;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsBrand;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Basic\GoodsCraft;
use App\Store\Models\Basic\GoodsPricetype;
use App\Store\Models\Basic\GoodsStone;
use App\Store\Models\Basic\GoodsStyle;
use App\Store\Models\Basic\PaytypeList;
use App\Store\Models\Basic\Supply;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Stock extends Model
{
    protected $table = 'site_oldmaterial_stock';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     * 列表
     */
    public static function getList($request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['storeinfo', 'goods_category', 'goods_pricetype', 'goods_brand'])->where('is_del', 1)->where('cp_id', $cp_id);

        $store_ids = StoreCommon::getCurrentAccountStoreId($request);
        if ($store_ids) { //权限控制
            if (is_array($store_ids)) { //获取当前权限下的门店
                $query->whereIn('store_id', $store_ids);
            }
            if (is_numeric($store_ids) && (int)$store_ids > 0) { //获取当前门店下的列表
                $query->where('store_id', (int)$store_ids);
            }
        }

        // 回收时间
        $start_time = is_numeric($request->start_time)? $request->start_time : strtotime($request->start_time);
        $end_time = is_numeric($request->end_time) > 0 ? $request->end_time : strtotime($request->end_time);

        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        //ids
        $ids = $request->ids;
        $ids = is_array($ids) ? $ids : (is_array(json_decode($ids, true)) ? json_decode($ids, true) : []);
        if ($ids != []) {
            $query->whereIn('id', $ids);
        }

        // 订单
        $order_sncode = $request->order_sncode ?? '';
        if ($order_sncode != '') {
            $query->where('order_sncode', 'like', '%' . $order_sncode . '%');
        }

        // 条码
        $goods_sn_code = $request->goods_sn_code ?? '';
        if ($goods_sn_code != '') {
            $query->where('goods_sn_code', 'like', '%' . $goods_sn_code . '%');
        }

        // 回收门店
        $store_id = $request->store_id ?? 0;
        if ($store_id > 0) {
            $query->where('store_id', $store_id);
        }

        // 品牌
        $goods_brand_id = $request->goods_brand_id ?? 0;
        if ($goods_brand_id > 0) {
            $query->where('goods_brand_id', $goods_brand_id);
        }

        // 状态
        $stock_status = $request->stock_status ?? 0;
        if ($stock_status > 0) {
            $query->where('stock_status', $stock_status);
        }

        // 过滤数量
        $is_count = $request->is_count ?? 0;
        if ($is_count > 0) {
            $query->where('count','>', 0);
        }

        // 类别
        $goods_category_id = $request->goods_catgory_id ?? 0;
        if ($goods_category_id > 0) {
            $query->where('goods_category_id', $goods_category_id);
        }
        $query->orderBy('id', 'desc');

        $isgetall = $request->isgetall ?? 0;

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        }

        $list = $query->get()->toArray();
        return ['list' => $list];
    }

    /**
     * 汇总列表
     */
    public static function getSummaryList($request)
    {
        // 按照门店、商品类别分组
        $query = self::with(['storeinfo', 'goods_category'])
            ->where(['cp_id' => $request['cp_id'],'is_del'=>1])
            ->groupBy('store_id', 'goods_category_id')
            ->select('store_id', 'goods_category_id', DB::raw('sum(count) as count,sum(piece_weight) as piece_weight,sum(piece_weight_new) as piece_weight_new,sum(exchange_amount) as exchange_amount,sum(valuations) as valuations'));

        //ids
        $ids = $request->ids;
        $ids = is_array($ids) ? $ids : (is_array(json_decode($ids, true)) ? json_decode($ids, true) : []);
        if ($ids != []) {
            $query->whereIn('id', $ids);
        }

        $store_ids = StoreCommon::getCurrentAccountStoreId($request);
        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }

        // 所属门店
        $store_id = $request->store_id ?? 0;
        if ($store_id > 0) {
            $query->where('store_id', $store_id);
        }

        // 过滤数量
        $is_count = $request->is_count ?? 0;
        if ($is_count > 0) {
            $query->where('count','>', 0);
        }

        $isgetall = $request->isgetall ?? 0;

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        }

        $list = $query->get()->toArray();
        return ['list' => $list];
    }

    /**
     * 关联类别
     */
    public function goods_category(){
        return $this->hasOne(GoodsCategory::class, 'id', 'goods_category_id')->select('id','category_name');
    }

    /**
     * 关联款式
     */
    public function goods_style(){
        return $this->hasOne(GoodsStyle::class, 'id', 'goods_style_id')->select('id','style_name');
    }

    /**
     * 关联店铺
     */
    public function storeinfo(){
        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id','store_name');
    }

    public function supply(){
        return $this->hasOne(Supply::class, 'id', 'supply_id')->select('id','supply_name');
    }

    public function goods_craft(){
        return $this->hasOne(GoodsCraft::class, 'id', 'goods_craft_id')->select('id','craft_name');
    }
    public function goods_pricetype(){
        return $this->hasOne(GoodsPricetype::class, 'id', 'goods_pricetype_id')->select('id','pricetype_name');
    }
    public function goods_stone(){
        return $this->hasOne(GoodsStone::class, 'id', 'goods_stone_id')->select('id','stone_name');
    }
    public function paytype_info(){
        return $this->hasOne(PaytypeList::class, 'id', 'goods_stone_id')->select('id','paytype_name');
    }

    public function goods_brand(){
        return $this->hasOne(GoodsBrand::class, 'id', 'goods_brand_id')->select('id','brand_name');
    }
}

